<?php
include("../db_connection.php");

include("timer.php");



//for debug

$_SESSION['topic'] = 2;
$_SESSION['class'] =2;
$_SESSION['level'] = 1;



if(!isset($_POST['qid'])){
}
else if($_POST['qid']=="new_qd"){

	if(!isset($_POST['new_qd'])){
	}
	else if($_POST['Type']==1){
		$_SESSION['posted']='yes';
		$new_qd = $_POST['new_qd'];
		$new_qd = str_replace("'", "&#39;", $new_qd);
		$new_qd = str_replace('"', '&quot;', $new_qd);
		$query = "insert into forum_question (question,type,timedate,color, user, topicid, level,classid) value ('".$new_qd."','1',DATE_ADD(NOW(),INTERVAL 8 HOUR),'".$_SESSION['color']."','".$_SESSION['user']."','".$_SESSION['topic']."','".$_SESSION['level']."','".$_SESSION['class']."')";
		mysql_query($query, $orcp_con) or die ('Error in query');
	}
	else if($_POST['Type']==2){
		$_SESSION['posted']='yes';
		$new_qd = $_POST['new_qd'];
		$new_qd = str_replace("'", "&#39;", $new_qd);
		$new_qd = str_replace('"', '&quot;', $new_qd);
		$query = "insert into forum_question (question,type,timedate,color, user, topicid, level,classid) value ('".$new_qd."','2',DATE_ADD(NOW(),INTERVAL 8 HOUR),'".$_SESSION['color']."','".$_SESSION['user']."','".$_SESSION['topic']."','".$_SESSION['level']."','".$_SESSION['class']."')";
		mysql_query($query, $orcp_con) or die ('Error in query');
	}
	else{
		echo "error";	
	}
}
else{
	if(!isset($_POST['new_rc'])){
	}
	else{
		if($_POST['RCType']==1){
			$_SESSION['posted']='yes';
			$new_rc = $_POST['new_rc'];
			$new_rc = str_replace("'", "&#39;", $new_rc);
			$new_rc = str_replace('"', '&quot;', $new_rc);
			$query = "insert into forum_reply (qid,repo,type,timedate,color, user, topicid, level,classid) value ('".$_POST['qid']."','".$new_rc."','1',DATE_ADD(NOW(),INTERVAL 8 HOUR),'".$_SESSION['color']."','".$_SESSION['user']."','".$_SESSION['topic']."','".$_SESSION['level']."','".$_SESSION['class']."')";
		 	mysql_query($query, $orcp_con) or die ('Error in query');
		}
		else if($_POST['RCType']==2){
			$_SESSION['posted']='yes';
			$new_rc = $_POST['new_rc'];
			$new_rc = str_replace("'", "&#39;", $new_rc);
			$new_rc = str_replace('"', '&quot;', $new_rc);
			$query = "insert into forum_reply (qid,repo,type,timedate,color, user, topicid, level,classid) value ('".$_POST['qid']."','".$new_rc."','2',DATE_ADD(NOW(),INTERVAL 8 HOUR),'".$_SESSION['color']."','".$_SESSION['user']."','".$_SESSION['topic']."','".$_SESSION['level']."','".$_SESSION['class']."')";
			mysql_query($query, $orcp_con) or die ('Error in query');
		}
		else{
			echo "error";	
		}	
	}
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sample page</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body onLoad="TimeCountDown()">
<div id="wrapper">
	<div id="header">
		<div id="logo"></div>
		<div id="menu">
        <?php
			include("menu.php");
		?>
        <!--
			<a href="">首頁</a> | <a href="">成績</a> | <a href="">註冊班級</a> | <a href="">帳號</a> | <a href="">線上聽力</a> | <a href="">聯絡我們</a> | <a href="">版本說明</a>
            !-->
		</div>
	</div>
	<div class="clearer"></div>
	<div class="edge-top"></div>

	<div id="main">

		<div id="title">Forum<font size=4><br>說明文字說明文字說明文字說明文字</font></div>
<hr/><br><br>

<!-- 內文開始 !-->


<?php



$query = "SELECT qid,question,type,color from forum_question WHERE topicid=".$_SESSION['topic']." AND classid = ".$_SESSION['class']." AND level=".$_SESSION['level']." order by timedate DESC";
//$query = "SELECT qid,question,type,color from forum_question order by timedate";

$result = mysql_query($query, $orcp_con) or die ('Error in query');
   
	$num = mysql_num_rows($result);
	while($row = mysql_fetch_row($result)){
		echo "<table width=700px style='background-color: #EFF5FF; border: solid 5px #EFF5FF'><tr valign=top><td width=50px>";
		if($row[2]==1){
			echo "<font  size=2pt face='Courier New' color = \"".$row[3]."\">[#".$row[3]."]</font>&nbsp;";
			echo "<td>Q: ".$row[1];
		}
		else if($row[2]==2){
			echo "<font  size=2pt face=\"Courier New\" color = \"".$row[3]."\">[#".$row[3]."]</font>&nbsp;";
			echo "<td>D: ".$row[1];
		}
		else 
			echo "error";
		echo "</table><table  width=700px style='background-color: #EFF5FF; border: solid 5px #EFF5FF' >";
		
		
		$query2 = "SELECT rid,repo,type,color FROM forum_reply WHERE qid='$row[0]' AND  topicid=".$_SESSION['topic']." AND classid = ".$_SESSION['class']." AND level=".$_SESSION['level']." order by timedate";
		//$query2 = "SELECT rid,repo,type,color FROM forum_reply WHERE qid ='$row[0]' order by timedate";
		$result2 = mysql_query($query2, $orcp_con) or die ('Error in query');
		$num2 = mysql_num_rows($result2);
		while($row2 = mysql_fetch_row($result2)){
			echo "<tr valign=top><td width=80px>";
			//echo "&emsp;&emsp;";
			if($row2[2]==1){
				echo "<td width=80px><font size=2pt face=\"Courier New\" color = \"".$row2[3]."\">[#".$row2[3]."]</font>&nbsp;";
				echo "<td><font size=4pt>R: ".$row2[1]."</font>";
			}
			else if($row2[2]==2){
				echo "<td width=50px><font size=2pt face=\"Courier New\" color = \"".$row2[3]."\">[#".$row2[3]."]</font>&nbsp;";
				echo "<td><font size=4pt>C: ".$row2[1]."</font>";
			}
			
		}
		
		echo "<form name='RC' action="."forum.php"." method='POST'>";
		echo "<input name='qid' type='hidden' value='$row[0]' >";

		echo "<tr><td width=50px><td width=50px><td><select name='RCType'>
		<option value=1>Reply
		<option value=2>Comment
		</select>";

		echo "<br><textarea name='new_rc' rows='2' cols='64'></textarea>";
		echo "<br><input type='submit' value = 'Submit'>";
		echo "</form>";
		echo "</table><br>";
		echo "<br>";
	}
?>



<!-- 內文結束 !-->

	</div>
    <div class="edge-bottom"></div>
    <div id="footer"><?php  include("footer.php"); ?></div>
</div>

<div id="txt">18:27</div>
<!--
<div id="association">&nbsp;association</div>
<div id="voc_cart">&nbsp;vocabulary cart</div> 
!-->
</body>
</html>